package com.zenithmind.version.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zenithmind.version.pojo.domain.Commit;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 提交Mapper接口
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Mapper
public interface CommitMapper extends BaseMapper<Commit> {

    /**
     * 分页查询提交列表
     * 
     * @param page 分页参数
     * @param repositoryId 仓库ID
     * @param branchId 分支ID
     * @param authorId 作者ID
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 提交列表
     */
    IPage<Commit> selectCommitPage(Page<Commit> page,
                                  @Param("repositoryId") String repositoryId,
                                  @Param("branchId") String branchId,
                                  @Param("authorId") String authorId,
                                  @Param("startTime") LocalDateTime startTime,
                                  @Param("endTime") LocalDateTime endTime);

    /**
     * 查询分支的提交列表
     * 
     * @param repositoryId 仓库ID
     * @param branchId 分支ID
     * @param limit 限制数量
     * @return 提交列表
     */
    List<Commit> selectByBranch(@Param("repositoryId") String repositoryId,
                               @Param("branchId") String branchId,
                               @Param("limit") Integer limit);

    /**
     * 根据哈希值查询提交
     * 
     * @param repositoryId 仓库ID
     * @param hash 提交哈希
     * @return 提交
     */
    Commit selectByHash(@Param("repositoryId") String repositoryId,
                       @Param("hash") String hash);

    /**
     * 查询用户的提交统计
     * 
     * @param repositoryId 仓库ID
     * @param authorId 作者ID
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 统计信息
     */
    CommitStatistics selectUserCommitStatistics(@Param("repositoryId") String repositoryId,
                                               @Param("authorId") String authorId,
                                               @Param("startTime") LocalDateTime startTime,
                                               @Param("endTime") LocalDateTime endTime);

    /**
     * 查询仓库的提交统计
     * 
     * @param repositoryId 仓库ID
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 统计信息
     */
    CommitStatistics selectRepositoryCommitStatistics(@Param("repositoryId") String repositoryId,
                                                     @Param("startTime") LocalDateTime startTime,
                                                     @Param("endTime") LocalDateTime endTime);

    /**
     * 查询提交趋势
     * 
     * @param repositoryId 仓库ID
     * @param days 天数
     * @return 趋势数据
     */
    List<CommitTrend> selectCommitTrend(@Param("repositoryId") String repositoryId,
                                       @Param("days") Integer days);

    /**
     * 提交统计信息
     */
    class CommitStatistics {
        private Long totalCommits;
        private Long addedLines;
        private Long deletedLines;
        private Long changedFiles;
        private String topAuthor;
        private Long topAuthorCommits;

        // Getters and Setters
        public Long getTotalCommits() { return totalCommits; }
        public void setTotalCommits(Long totalCommits) { this.totalCommits = totalCommits; }
        
        public Long getAddedLines() { return addedLines; }
        public void setAddedLines(Long addedLines) { this.addedLines = addedLines; }
        
        public Long getDeletedLines() { return deletedLines; }
        public void setDeletedLines(Long deletedLines) { this.deletedLines = deletedLines; }
        
        public Long getChangedFiles() { return changedFiles; }
        public void setChangedFiles(Long changedFiles) { this.changedFiles = changedFiles; }
        
        public String getTopAuthor() { return topAuthor; }
        public void setTopAuthor(String topAuthor) { this.topAuthor = topAuthor; }
        
        public Long getTopAuthorCommits() { return topAuthorCommits; }
        public void setTopAuthorCommits(Long topAuthorCommits) { this.topAuthorCommits = topAuthorCommits; }
    }

    /**
     * 提交趋势
     */
    class CommitTrend {
        private String date;
        private Long commits;
        private Long addedLines;
        private Long deletedLines;

        // Getters and Setters
        public String getDate() { return date; }
        public void setDate(String date) { this.date = date; }
        
        public Long getCommits() { return commits; }
        public void setCommits(Long commits) { this.commits = commits; }
        
        public Long getAddedLines() { return addedLines; }
        public void setAddedLines(Long addedLines) { this.addedLines = addedLines; }
        
        public Long getDeletedLines() { return deletedLines; }
        public void setDeletedLines(Long deletedLines) { this.deletedLines = deletedLines; }
    }
}
